/*
 * funciones.c
 *
 *  Created on: 18/10/2011
 *      Author: utn_so
 */

#include "funciones.h"






char* parseacomando (char* cadena)
{
      char* copia = (char*) malloc (64);
      char* token = (char*) malloc (64);

      strcpy (copia,cadena);

      token = strtok (copia," ");
      cadena = token;
      token = NULL;
      free(token);
      return cadena;

}

char* parseaparametro (char* cadena)
{
      char* copia = (char*) malloc (64);
      char* dato = (char*) malloc (64);
      int32_t i = 0;
      int32_t j = 0;

      strcpy (copia,cadena);

      while( copia[i] != ' ' )
      {
    	  i++;
      }
      i++;

      while( copia[i] != '\0' ) {

             dato[j] = copia[i];
             i++;
             j++;

             }
      dato[j] = '\0';
      strcpy(cadena,dato);
      free(dato);
      free(copia);
      return cadena;
}


int32_t conectarC(char* ip, char* port)
{
	struct sockaddr_in their_addr;  /* informacion de la direccion de destino VSK father*/
	int32_t sockfd;

	if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
	{
		sockfd = -2;
		perror("Error: No se pudo crear el socket return...");
		return sockfd;
	}

    their_addr.sin_family = AF_INET;    /* Ordenacion de bytes de la maquina*/
	their_addr.sin_port = htons(atoi(port));  /* short, Ordenacion de bytes de la red*/
	their_addr.sin_addr.s_addr = inet_addr(ip);
	memset(&(their_addr.sin_zero),'\0', 8);  /* poner a cero el resto de la estructura*/

    if (connect(sockfd, (struct sockaddr *)&their_addr,sizeof(their_addr))<0)
	{
		sockfd = -3;
		perror("\nError: No se pudo realizar el connect...");
		return sockfd;
	}
    return sockfd;
}

int32_t initSockGral(void)
{
    int32_t sockfd=1;

	if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
		return -1;

	struct sockaddr_in my_addr;				/*	informaci�n sobre mi direcci�n	*/
	my_addr.sin_family = AF_INET; 			/*	Ordenaci�n de bytes de la m�quina	*/
	my_addr.sin_port = htons(MYPORT); 		/*	short, Ordenaci�n de bytes de la red	*/
	my_addr.sin_addr.s_addr = INADDR_ANY; 	/*	Rellenar con mi direcci�n IP	*/
	memset(&(my_addr.sin_zero), '\0', 8);	/*	Poner a cero el resto de la estructura	*/

	if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))== -1)
		return -1;

    if ( listen(sockfd, BACKLOG) < 0 )
       return -1;

	return sockfd;
}

int32_t initSockConsole (void)
{
    int32_t sock=-1;
    struct sockaddr_un serveraddr;

    sock = socket(AF_UNIX, SOCK_STREAM, 0);
    if (sock < 0)
       {
       perror("socket() UNIX failed\n");
       return -1;
       }
    memset(&serveraddr, 0, sizeof(serveraddr));
    serveraddr.sun_family = AF_UNIX;
    strcpy(serveraddr.sun_path, SERVER_PATH);
    unlink(serveraddr.sun_path);
    if ( bind(sock, (struct sockaddr *)&serveraddr, SUN_LEN(&serveraddr)) < 0 ){
       perror("bind() UNIX failed\n");
       return -1;
    }
    if ( listen(sock, 10) < 0 ){
       perror("listen() UNIX failed\n");
       return -1;
    }
    return sock;
}

/*--------------------------------------------------------------
// itoa()
//      Integer to ascii
//--------------------------------------------------------------*/
char* itoa(int32_t num)
{
	char *saux;
	char s[5+1];
	sprintf(s,"%i",num);
	saux = (char*)malloc(strlen(s)+1);
	strcpy(saux,s);
	return saux;
}

char *substring(size_t start, size_t stop, const char *src, char *dst, size_t size)
{
	int32_t count = stop - start;
	if ( count >= --size )
		{
		count = size;
		}
	sprintf(dst, "%.*s", count, src + start);
	return dst;
}


void copiar(char* orig, char *dest, int32_t size){
	for(int32_t i=0; i<size; i++)
		dest[i]=orig[i];
}


int comparar(char *cad1, char *cad2, int32_t size){
	for (int32_t i=0; i<size; i++)
		if(cad1[i]!=cad2[i])
			return -1;
	return 0;
}
